﻿using IdentityModel.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfClient
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private string accessToken = string.Empty;

        private DiscoveryDocumentResponse disco;

        public MainWindow()
        {
            InitializeComponent();
        }

        private async void btnAccessToken_Click(object sender, RoutedEventArgs e)
        {
            var userName = this.txtName.Text;
            var password = this.txtPwd.Text;

            var client = new HttpClient();
            disco = await client.GetDiscoveryDocumentAsync("http://localhost:5000");
            if (disco.IsError)
            {
                Console.WriteLine(disco.Error);
                return;
            }

            var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest()
            {
                Address = disco.TokenEndpoint,
                ClientId = "wpfClient",
                ClientSecret = "wpfClient",
                Scope = "api1 openid profile address email phone",

                UserName = userName,
                Password = password
            });
            if (tokenResponse.IsError)
            {
                MessageBox.Show(tokenResponse.Error);
                return;
            }

            this.accessToken = tokenResponse.AccessToken;
            this.txtAccessToken.Text = tokenResponse.Json.ToString();
        }

        private async void btnResponse_Click(object sender, RoutedEventArgs e)
        {
            var apiClient = new HttpClient();
            apiClient.SetBearerToken(this.accessToken);
            var response = await apiClient.GetAsync("http://localhost:5011/WeatherForecast");
            if (!response.IsSuccessStatusCode)
            {
                MessageBox.Show(response.StatusCode.ToString());
            }
            else
            {
                this.txtResponse.Text = await response.Content.ReadAsStringAsync();
            }
        }

        private async void btnIdentityResponse_Click(object sender, RoutedEventArgs e)
        {
            var apiClient = new HttpClient();
            apiClient.SetBearerToken(this.accessToken);
            var response = await apiClient.GetAsync(this.disco.UserInfoEndpoint);
            if (!response.IsSuccessStatusCode)
            {
                MessageBox.Show(response.StatusCode.ToString());
            }
            else
            {
                this.txtIdentityResponse.Text = await response.Content.ReadAsStringAsync();
            }
        }
    }
}
